Count Property Example

This example demonstrates the Count property with three different collections in the Northwind database. The property obtains the number of objects in each collection, and sets the upper limit for loops that enumerate these collections. Another way to enumerate these collections without using the Count property would be to use For Each...Next statements.

Sub CountX()

    Dim dbsNorthwind As Database
    Dim intloop As Integer

    Set dbsNorthwind = OpenDatabase("Northwind.mdb")

    With dbsNorthwind
        ' Print information about TableDefs collection.
        Debug.Print .TableDefs.Count & _
            " TableDefs in Northwind"
        For intloop = 0 To .TableDefs.Count - 1
            Debug.Print "  " & .TableDefs(intloop).Name
        Next intloop

        ' Print information about QueryDefs collection.
        Debug.Print .QueryDefs.Count & _
            " QueryDefs in Northwind"
        For intloop = 0 To .QueryDefs.Count - 1
            Debug.Print "  " & .QueryDefs(intloop).Name
        Next intloop

        ' Print information about Relations collection.
        Debug.Print .Relations.Count & _
            " Relations in Northwind"
        For intloop = 0 To .Relations.Count - 1
            Debug.Print "  " & .Relations(intloop).Name
        Next intloop

        .Close
    End With

End Sub